package com.bos.service.impl;

import com.bos.dao.repositories.PromotionRepository;
import com.bos.domian.base.BaseQuery;
import com.bos.domian.base.Promotion;
import com.bos.service.PromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author misterWei
 * @create 2018年11月20号:14点44分
 * @mailbox mynameisweiyan@gmail.com
 */
@Service
@Transactional
public class PromotionServiceImpl  implements PromotionService{
    @Autowired
    private PromotionRepository promotionRepository;

    @Override
    public void save(Promotion model) {
        promotionRepository.save(model);
    }

    @Override
    public Map<String, Object> promotionPageQuery(int i, Integer rows) {
        Page<Promotion> all = promotionRepository.findAll(new PageRequest(i, rows));
        Map<String,Object> takeTimeList  = new HashMap<>();
        takeTimeList.put("total",all.getTotalElements());
        takeTimeList.put("rows",all.getContent());
        return takeTimeList;
    }

    @Override
    public BaseQuery<Promotion> pageQuery(int page, int rows) {
        BaseQuery<Promotion> baseQuery = new BaseQuery<>();

        Page<Promotion> all = promotionRepository.findAll(new PageRequest(page-1, rows));

        baseQuery.setQueryList(all.getContent());
        baseQuery.setTotalElements(all.getTotalElements());
        baseQuery.setCurrentlyPages(all.getNumber());
        baseQuery.setPages(all.getTotalPages());

        return baseQuery;
    }

    @Override
    public Promotion findOne(Integer id) {
        return promotionRepository.findOne(id);
    }

    @Override
    public void updateStatus(Date date) {
        promotionRepository.uplodateStatus(date);
    }
}
